package org.easy.tool.common;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author Valley
 * Created by Valley on 2023/7/14 17:08
 */
public class StringUtil {

    /**
     * 功能：判断字符串是不是数字
     */
    public static boolean isNumberStr(String str) {
        return str != null && str.chars().allMatch(Character::isDigit);
    }

    /**
     * 功能：对字符串进行unicode编码
     */
    public static String unicodeStr(String originStr) throws Exception {
        char[] utfBytes = originStr.toCharArray();
        StringBuilder unicodeBytes = new StringBuilder();
        for (char utfByte : utfBytes) {
            String hexB = Integer.toHexString(utfByte);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes.append("[").append(hexB).append("]");
        }
        return unicodeBytes.toString();
    }

    /**
     * 功能：只对字符串中的中文进行unicode编码
     */
    public static String unicodeStrCN(String originStr) throws Exception {
        char[] utfBytes = originStr.toCharArray();
        StringBuilder unicodeBytes = new StringBuilder();
        for (char utfByte : utfBytes) {
            String hexB = Integer.toHexString(utfByte);
            if (hexB.length() <= 2) {
                unicodeBytes.append(utfByte);
            }else {
                unicodeBytes.append("[").append(hexB).append("]");
            }
        }
        return unicodeBytes.toString();
    }

    /**
     * 功能：替换指定前缀和后缀之间的字符串为某个值
     */
    public static String replaceStr(String origin, String pre, String suf, String value) {
        String[] split = origin.split(pre);
        //如果前缀错误
        if (split.length == 1) return origin;
        String[] sonSplit = split[1].split(suf);
        //如果后缀错误
        if (sonSplit.length == 1) return split[0] + pre + value;
        return split[0] + pre + value + suf + sonSplit[1];
    }

    /**
     * 功能：从指定索引开始截取指定数量字符
     */
    public static String sub(String str, int index, int len) {
        return StrUtil.subWithLength(str, index, len);
    }

    /**
     * 功能：判断两个字符串是否相等
     */
    public static boolean equals(String str1, String str2) {
        return StrUtil.equals(str1, str2);
    }

    /**
     * 功能：判断当前字符串不是空，也不是null
     */
    public static boolean isNotBlank(String str) {
        return StrUtil.isNotBlank(str);
    }

    /**
     * 功能：字符串格式化拼接
     */
    public static String format(String template, Object... args) {
        return StrUtil.format(template, args);
    }

    /**
     * 功能：生成32位随机ID
     */
    public static String randomID() {
        return IdUtil.simpleUUID();
    }

    /**
     * 功能：生成指定长度随机字符串
     */
    public static String random(int len) {
        return RandomUtil.randomString(len);
    }

    /**
     * 功能：取出来所有指定前缀和指定后缀之间的值
     */
    public static List<String> getPreSufStr(String origin, String pre, String suf) {
        List<String> strings = new ArrayList<>();
        if (origin.contains(pre) && origin.contains(suf)) {
            String mark = "PRE_SUF";
            origin = "start" + origin + "end";
            origin = origin.replace(pre, mark)
                    .replace(suf, mark);
            String[] split = origin.split(mark);
            for (int i = 1; i < split.length - 1; i += 2) {
                strings.add(split[i]);
            }
        }
        return strings;
    }

    /**
     * 功能：取出来所有指定前缀和指定后缀分隔开的值，不忽略开头结尾
     */
    public static List<String> getStrByPreSuf(String origin, String pre, String suf) {
        //使用方式
        /*LinkedHashMap<String, List<String>> nameMap = new LinkedHashMap<>();
        for (int i = 1; i <= preSufStr.size(); i++) {
            if (i % 2 == 0) {
                String dbName = preSufStr.get(i-1);
                nameMap.put(dbName, getDataConn(dbName));
            }
        }*/
        List<String> strings = new ArrayList<>();
        if (origin.contains(pre) && origin.contains(suf)) {
            String mark = "PRE_SUF";
            origin = origin.replace(pre, mark)
                    .replace(suf, mark);
            Collections.addAll(strings, origin.split(mark));
        }
        return strings;
    }


}
